package com.huya.k12.translate.model.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class StandaloneWriter extends XMLWriter {
	public StandaloneWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException{
		super(out, format);
	}

	protected void writeDeclaration() throws IOException {
		OutputFormat format = getOutputFormat();

		String encoding = format.getEncoding();

		if (!format.isSuppressDeclaration()) {
			if (encoding.equals("UTF8")) {
				writer.write("<?xml version=\"1.0\"");

				if (!format.isOmitEncoding()) {
					writer.write(" encoding=\"utf-8\"");
				}

				writer.write(" standalone=\"no\"");
				writer.write("?>");
			} else {
				writer.write("<?xml version=\"1.0\"");

				if (!format.isOmitEncoding()) {
					writer.write(" encoding=\"" + encoding + "\"");
				}

				writer.write(" standalone=\"no\"");
				writer.write("?>");
			}
		}
	}
}